package com.ncloud.timetask.dashboard.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ncloud.common.enmus.ResultEnum;
import com.ncloud.common.exception.UserException;
import com.ncloud.timetask.dashboard.mapper.DashboardChnlTaskMapper;
import com.ncloud.timetask.dashboard.service.DashboardChnlTaskService;

/**
 * @author sundelong
 * project_name: timetask 
 * type_name: DashboardChnlTaskServiceImpl 
 * date: 2017年6月21日下午5:10:59 
 * 
 */

@Service
public class DashboardChnlTaskServiceImpl implements DashboardChnlTaskService {
	@Autowired
	DashboardChnlTaskMapper chnlMapper;
	
	@Override
	public void insertChnlSaleCount(String taskId){
		List<Map<Object,Object>> countList=new ArrayList<Map<Object,Object>>();
		try {				
			countList=chnlMapper.getSumChnlByMonth();
			String year = ThisMonth().get("year");
			String month = ThisMonth().get("month");
			String remark = null;
			for(int i=0;i<countList.size();i++){
				Map<Object,Object> map=new HashMap<Object, Object>();
				map=countList.get(i);
				String chnlId=map.get("chnl_id").toString();						
				boolean flag=map.containsKey("sum");
				String chnlIncome = null;
				if(flag){
					chnlIncome=map.get("sum").toString();
					}
				//判断这个月份的数据有没有插入表中，如果已插入就更新数据，没有就插入；
				int count = chnlMapper.getExistence(taskId, year, month,chnlId);
				if(count == 0){
					chnlMapper.insertChnlIncome(taskId, year, month, chnlId, chnlIncome, remark);
				}else{
					chnlMapper.updateBsMonthlyTask(chnlIncome, taskId, year, month, chnlId);
				}
			}
									
		} catch (Exception e) {
			e.printStackTrace();
			throw new UserException(ResultEnum.DATABASE_ERROR);
		}
	}

	@Override
	public void insertProductCount(String taskId){
		List<Map<Object,Object>> countList=new ArrayList<Map<Object,Object>>();
		try {				
			countList=chnlMapper.getSaleProductFee();
			String remark = null;
			for(int i=0;i<countList.size();i++){
				Map<Object,Object> map=new HashMap<Object, Object>();
				map=countList.get(i);
				String userId=map.get("user_id").toString();
				
				boolean flag1=map.containsKey("sale_name");
				String saleName = null;
				if(flag1){
					saleName=map.get("sale_name").toString();
					}
				boolean flag2=map.containsKey("prod_name");
				String prodName = null;
				if(flag2){
					prodName=map.get("prod_name").toString();
					}
				boolean flag3=map.containsKey("fee");
				String fee = null;
				if(flag3){
					fee=map.get("fee").toString();
					}
				
				//判断这个销售的数据有没有插入表中，如果已插入就更新数据，没有就插入；
				int nullName = 0;
				if(prodName == null){
				 nullName=chnlMapper.getExistenceByUserIdNull(taskId, prodName, userId);
				}
				int count = chnlMapper.getExistenceByUserId(taskId, prodName, userId);
				if(count == 0 && nullName == 0){
					chnlMapper.insertProductCount(userId, taskId, saleName, prodName, fee, remark);
				}else{
					if(nullName != 0){
						chnlMapper.updateProductCountNull(fee, prodName, userId);
					}else{
					chnlMapper.updateProductCount(fee, userId, prodName);
					}
				}
			}
									
		} catch (Exception e) {
			e.printStackTrace();
			throw new UserException(ResultEnum.DATABASE_ERROR);
		}
	}	
	
	public Map<String,String> LastMonth(){
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONTH, -1);
		SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM");
		String time = format.format(cal.getTime());
		String year = time.split("-")[0];
		String day = time.split("-")[1];
		Map<String,String> dateMap = new HashMap<String, String>();
		dateMap.put("year", year);
		dateMap.put("month",day);
		return dateMap;		
	}
	public Map<String, String> ThisMonth(){
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		String time = sdf.format(date);
		String year = time.split("-")[0];
		String day = time.split("-")[1];
		Map<String,String> dateMap = new HashMap<String, String>();
		dateMap.put("year", year);
		dateMap.put("month",day);
		return dateMap;		
	}	
}
